<template>
  <div>
    <van-popup position="right" :value="isShow" @open="close">
      <van-nav-bar left-arrow title="123" @click-left="leftClick">
        <template #title> {{ total }}条回复 </template>
      </van-nav-bar>
      <div class="reply-wrapper list">
        <!-- 列表 -->
        <van-list
          v-if="bol"
          v-model="loading"
          finished-text="没有更多了"
          :finished="finished"
          @load="getData"
        >
          <div
            class="item van-hairline--bottom"
            v-for="i in list"
            :key="i.com_id"
          >
            <van-image round width="10vw" height="10vw" :src="i.aut_photo" />
            <div class="info">
              <p>
                <span class="name">{{ i.aut_name }}</span>
                <span class="zan"
                  >{{ i.like_count }} <geek-icon name="like2"
                /></span>
              </p>
              <p class="cont">{{ i.content }}</p>
              <p>
                <span class="time" style="margin-left:0">{{
                  i.pubdate | filterTime
                }}</span>
              </p>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 底 -->
      <div class="footer van-hairline--top" style="position:static">
        <div class="input big" @click="inputClick">
          <i class="van-icon van-icon-edit"></i>
        </div>
        <div class="btn">
          <geek-icon name="collect"></geek-icon>
          <p>收藏</p>
        </div>
        <div class="btn">
          <geek-icon name="share"></geek-icon>
          <p>分享</p>
        </div>
      </div>
    </van-popup>
    <van-popup
      position="bottom"
      v-model="sendShow"
      v-if="bol2"
      @close="closeEvent"
    >
      <van-nav-bar
        left-arrow
        right-text="发表"
        :title="localItem !== '' ? '回复评论' : '发表评论'"
        @click-left="sendShow = false"
      >
        <template #right>
          <div @click="send" style="color:#f40">
            发表
          </div>
        </template>
      </van-nav-bar>
      <van-form ref="form">
        <van-field
          :rules="rules"
          v-model="text"
          rows="3"
          autosize
          type="textarea"
          maxlength="100"
          placeholder="请输入内容"
          show-word-limit
        />
      </van-form>
    </van-popup>
  </div>
</template>

<script>
import { comments, commentsPost } from '@/api/article.js'
export default {
  name: 'ReComment',
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    localItem: {
      type: [String, Object],
      default: ''
    },
    commList: {
      type: Array,
      default: () => []
    },
    info: {
      type: [String, Object],
      default: ''
    }
  },
  model: {
    prop: 'isShow'
  },
  data () {
    return {
      bol2: true,
      text: '',
      sendShow: false,
      total: 0,
      bol: true,
      loading: false,
      finished: false,
      params: {
        type: 'c',
        source: '',
        offset: null,
        limit: 10
      },
      list: [],
      rules: [{ required: true, message: '必填', trigger: 'onChange' }]
    }
  },
  created () {
    // this.getData()
  },
  methods: {
    leftClick () {
      this.$emit('input', false)
    },
    async getData () {
      const res = await comments({
        ...this.params,
        source: this.localItem.com_id
      })
      this.loading = false
      this.list.push(...res.data.results)
      this.total = this.list.length
      if (res.data.end_id === res.data.last_id) {
        this.finished = true
      } else {
        this.params.offset = res.data.last_id
      }
      console.log('回复', res)
    },
    close () {
      this.list = []
      this.loading = false
      this.finished = false
      this.params.offset = null
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    // 弹出评论回复
    inputClick () {
      this.sendShow = true
    },
    // 发送
    async send () {
      if (this.localItem !== '') {
        await this.$refs.form.validate()
        const res = await commentsPost({
          target: this.localItem.com_id,
          content: this.text,
          art_id: this.$route.params.id
        })
        this.list.unshift(res.data.new_obj)
        this.total = this.list.length
      } else {
        await this.$refs.form.validate()
        const res = await commentsPost({
          target: this.$route.params.id,
          content: this.text
        })
        this.commList.unshift(res.data.new_obj)
        this.info.comm_count++
      }
      this.sendShow = false
    },
    closeEvent () {
      this.text = ''
      this.bol2 = false
      this.$nextTick(() => {
        this.bol2 = true
      })
    }
  }
}
</script>

<style lang="less" scoped>
.van-popup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.reply-wrapper {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

.list {
  padding: 0 16px 30px;
  .item {
    display: flex;
    padding: 10px 0;
    .info {
      padding-left: 10px;
      flex: 1;
      p {
        margin: 0;
        .name {
          font-size: 16px;
        }
        .zan {
          font-size: 14px;
          float: right;
          color: #999;
          .geek-icon {
            font-size: 12px;
            position: relative;
            top: -1px;
          }
        }
        &.cont {
          font-size: 14px;
          color: #666;
          padding: 10px 0;
          word-break: break-all;
          padding-right: 40px;
        }
        .reply {
          min-width: 60px;
          height: 24px;
          text-align: center;
          line-height: 28px;
          font-size: 12px;
          background: @geek-gray-color;
          display: inline-block;
          border-radius: 14px;
          color: #666;
          .van-icon {
            position: relative;
            top: 1px;
          }
        }
        .time {
          font-size: 12px;
          color: #999;
          margin-left: 10px;
        }
      }
    }
  }
}

.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  background: #fff;
  display: flex;
  width: 100%;
  align-items: center;
  .input {
    margin-left: 10px;
    width: 200px;
    height: 34px;
    background: @geek-gray-color;
    border-radius: 17px;
    line-height: 36px;
    padding-left: 10px;
    .van-icon {
      color: #999;
    }
    &.big {
      width: 260px;
    }
  }
  .btn {
    flex: 1;
    text-align: center;
    position: relative;
    p {
      margin: 0;
      font-size: 10px;
    }
    .geek-icon {
      font-size: 18px;
    }
    i {
      height: 16px;
      min-width: 16px;
      padding: 0 3px;
      background: @geek-color;
      color: #fff;
      font-size: 10px;
      position: absolute;
      right: 0;
      top: -4px;
      line-height: 16px;
      border-radius: 8px;
      font-style: normal;
    }
  }
}
::v-deep .van-nav-bar__text {
  color: @geek-color;
}
::v-deep .van-field__control {
  background: @geek-gray-color;
  padding: 5px 10px;
  margin-bottom: 5px;
  border-radius: 4px;
}
</style>
